<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <parent-div></parent-div>
  </div>
  <!-- 父组件模板 -->
  <template id="parent">
    <div style="width: 300px;height: 150px;background-color: coral;">
      <p>{{parentMsg}}</p>
      <child1></child1>
    </div>
  </template>
  <!-- 子组件1 -->
  <template id="child1">
    <div style="background-color: cornflowerblue;">
      {{child1Msg}}
      {{name}}
      {{age}}
      {{msg}}
    </div>
  </template>
  <script src="../vue.js"></script>
  <script>
    const child1 = {
      template: '#child1',
      data() {
        return {
          child1Msg: '我是child1'
        }
      },
      inject: ['name', 'age', 'msg'],
      mounted() {
        console.log(this.age);
        console.log(this.name);
        console.log(this.msg);
      }
    }
    const parent = {
      template: '#parent',
      data() {
        return {
          parentMsg: '我是父组件'
        }
      },
      /* 
        provide和inject用于父向后代传递数据，父组件可以将数据传给孙组件
        provide以对象的形式将数据发送出去，后代组件以数组的形式接受
      */
      provide() {
        return {
          name: '白粉花',
          age: 15,
          msg: this.parentMsg
        }
      },
      //模板编译完成阶段
      mounted() {
        console.log(this);
      },
      components: {child1}
    }
    Vue.component('ParentDiv', parent);
    const app = new Vue({
      el: '#app'
    })
  </script>
</body>

</html>